﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

public partial class Controls_Reports_PostStats : System.Web.UI.UserControl
{
	public long PostID { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
		if(!IsPostBack)
			LoadData();		
    }

	public override void DataBind()
	{
		base.DataBind();

		if (IsPostBack)
		{
			LoadData();
		}
	}

	private void LoadData()
	{
		var post = BLL.Posts.GetByID(PostID);
		if (post == null)
			return;

		lblFbComments.Text = post.FbCommentNum.ToString();
		lblTwreplys.Text = post.TwReplyNum.ToString();
		lblTwRetweets.Text = post.TwRetweetNum.ToString();
		lblDate.Text = post.TimePosted.ToString(CultureInfo.InvariantCulture);
		SetTitleText(post);
		SetLocationPostedText(post);

		var data = new List<List<string>>();

		var itemDailyStats = Logic.BusinessLogic.Stats.GetPostDailyStats(PostID);

		var fbComments = new List<string>();
		var replys = new List<string>();
		var retweets = new List<string>();

		foreach (var state in itemDailyStats)
		{
			fbComments.Add(state.FbCommentNum.ToString());
			replys.Add(state.TwReplyNum.ToString());
			retweets.Add(state.TwRetweetNum.ToString());
		}

		data.Add(fbComments);
		data.Add(replys);
		data.Add(retweets);
		//chart.LoadData(data);
		amLine.ExternalDataFileUrl = "/Service/websiteservice.asmx/PostTimeLine?postID=" + post.ID.ToString();
	}

	private void SetLocationPostedText(BO.Post post)
	{
		if (!string.IsNullOrEmpty(post.FbItemID))
            lblPostedOn.CssClass += " FacebookIconSmall";
		if (!string.IsNullOrEmpty(post.TwStatusId))
            lblPostedOn.CssClass += " TwitterIconSmall";
	}

	private void SetTitleText(BO.Post post)
	{
		string text = "no title";
		if (!string.IsNullOrEmpty(post.Title))
			text = post.Title;
		else 
			if(!string.IsNullOrEmpty(post.Text))
				text = post.Text;


		if (text.Length > 200)
			text.Remove(200);
		lblTitle.Text = text;
	}
}